<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<style>
    .ibox-content {
        min-height: 300px;
    }

    .form-group {
        display: flex;
        align-items: center;
    }

    .control-label {
        text-align: right;
        width: 100px;
    }

    .form-control {
        padding: 4px 12px;
    }

    .col-sm-offset-3 {
        margin: 0px auto !important;
    }
</style>
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">数据来源：</label>
                        <div class="col-sm-8" style="width: 62%;">
                            <select id="type" name="type" class="form-control">
                                <option value="">请选择</option>
                                <option value="0">后台</option>
                                <option value="1">小程序</option>
                                <option value="2">课程</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">导入：</label>
                        <div class="col-sm-8">
                            <form id="importExcelForm" method="post"
                                  enctype="multipart/form-data">
                                <input class="form-control" name="file" id="fileUrl" type="file">
                            </form>
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 140px">
                        <div class="col-sm-8 col-sm-offset-3" style="display: flex;">
                            <button type="submit" class="btn btn-primary" style="margin: 0px auto;
    display: block;"
                                    onclick="importExcel($('#fileUrl'),$('#importExcelForm'),$('#type').val(),'/sys/user/import')">
                                提交
                            </button>
                            <button type="submit" class="btn" style="margin: auto 0px auto 50px;
    display: block;"
                                    onclick="closeDialog()">
                                关闭
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
<div th:include="include::footer"></div>
<script type="text/javascript" src="/js/webJs/sys/user/importUser.js?r=1">
</script>
<script type="text/javascript" src="/js/jquery-form.js"></script>
</body>
</html>
<script>
    function closeDialog() {
        var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
        parent.layer.close(index);
    }

    //导入
    function importExcel(fileObj, formObj, eBatchId, url) {
        if (eBatchId == null || eBatchId == '') {
            layer.msg('请选择数据来源后后导入!');
            console.log(21231)
        } else {
            var fileUrl = fileObj.val();
            var suffix = fileUrl.substr(fileUrl.lastIndexOf(".")).toLowerCase();//获得文件后缀名
            if (suffix == '.xls' || suffix == '.xlsx') {
                if (fileUrl) {
                    console.log(fileUrl)
                    console.log(444)
                    console.log(formObj)
                    formObj.ajaxSubmit({
                        type: "post",
                        url: url,
                        data: {userSource: eBatchId},
                        dataType: "json",
                        success: function (data) {
                            console.log("data", data);
                            if (data.code == 0) {
                                parent.layer.msg("导入成功");
                                parent.reLoad();
                                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                                parent.layer.close(index);
                            } else if (data.code == 41304) {
                                layer.confirm('是否下载错误报告？', {
                                    btn1: function (index) {
                                        window.location.href = '/sys/user/importError';
                                        layer.close(index)
                                    }
                                });
                            } else {
                                layer.msg(data.msg);
                            }
                        }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest, textStatus, errorThrown)
                            layer.msg('出错了!');
                        }
                    });
                } else {
                    layer.msg('上传的文件不可为空');
                    // parent.$.messager.alert('警告', "上传的文件不可为空", 'warning');
                }
            } else {
                layer.msg('请上传正确的Excel表格!');
                // $.messager.alert('警告', "", 'warning');
            }
        }
    }


</script>